package com.hugy.test.service;

import com.hugy.test.model.TestUser;
import com.hugy.test.model.UserDTO;
import com.hugy.test.repo.TestUserRepository;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class MongoDBTestServiceImpl implements MongoDBTestService {

	@Resource
	private MongoTemplate mongoTemplate;

	@Resource
	private TestUserRepository testUserRepository;

	@Override
	public void addMongoDB(TestUser user) {
		//添加到Mongo数据库时，默认以对象userDTO为集合名称添加
		testUserRepository.save(user);
		//也可以指定添加到某个集合中
		//mongoTemplate.insert(user, "testCollection");
	}

	@Override
	public void addMongoDB(UserDTO user) {
		//添加到Mongo数据库时，默认以对象userDTO为集合名称添加
  		mongoTemplate.insert(user);
  		//也可以指定添加到某个集合中
  		//mongoTemplate.insert(user, "testCollection");
	}
	@Override
	public void deleteMongoDB(String userName) {
		//根据姓名删除
		Query query = Query.query(Criteria.where("userName").is(userName));
		mongoTemplate.remove(query, UserDTO.class);
	}

	@Override
	public void updateMongoDB(TestUser user) {
		//更改一条数据的一个字段
		Query query = Query.query(Criteria.where("userName").is(user.getUserName()));
		Update update = Update.update("age", user.getAge());
		//参数1:查询条件  参数2:修改  参数3:表名称（可以传对应集合的对象名）
		mongoTemplate.updateFirst(query, update, TestUser.class);
	}

	@Override
	public void updateManyMongoDB(TestUser user) {
		//更改一条数据的多个字段
		Query query = Query.query(Criteria.where("userName").is("123"));
		Update update = new Update();
		update.set("age", user.getAge());
		update.set("userName", "test2");
		//参数1:查询条件  参数2:修改  参数3:表名称（也可以是具体集合名）
		mongoTemplate.updateFirst(query, update, "testUser");
	}
	@Override
	public TestUser getOneMongoDB(String userName) {
		Query query = Query.query(Criteria.where("userName").is(userName));
		//findOne() 返回一个文档满足指定的查询条件。如果有多个文档满足查询条件，则返回第一个文档
		TestUser user = mongoTemplate.findOne(query, TestUser.class);
		return user;
	}

	public List<TestUser> getManyMongoDB(String userName) {
		Query query = Query.query(Criteria.where("userName").is(userName));
		//find() 返回所有满足条件的文档
		List<TestUser> testUserList = mongoTemplate.find(query, TestUser.class, "testUser");
		return testUserList;
	}

	@Override
	public List<TestUser> getAllMongoDB() {
		//findAll() 返回集合中所有的文档
		List<TestUser> testUserList = mongoTemplate.findAll(TestUser.class, "testUser");
		return testUserList;
	}

	@Override
	public List<TestUser> getFuzzyMongoDB(String userName) {
		//设置正则规则
		//Pattern.CASE_INSENSITIVE 启用不区分大小写的匹配
		Pattern pattern = Pattern.compile("^.*" + userName +".*$", Pattern.CASE_INSENSITIVE);
		Query query = Query.query(Criteria.where("userName").regex(pattern));
		List<TestUser> testUserList = mongoTemplate.find(query, TestUser.class);
		return testUserList;
	}

	@Override
	public List<UserDTO> getpageMongoDB() {
		Query query = new Query();
		//跳过10条，每页显示5条
		query.skip(10).limit(5);
		List<UserDTO> testUserList = mongoTemplate.find(query, UserDTO.class, "user");
		//总个数
		long count = mongoTemplate.count(query, "user");
		System.out.println(count);
		return testUserList;
	}
}

